package com.zbkj.crmeb.merchant.admin.controller;

import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.crmeb.core.common.CommonResult;
import com.zbkj.crmeb.agent.model.Agent;
import com.zbkj.crmeb.agent.service.AgentService;
import com.zbkj.crmeb.enums.BrandTypeEnum;
import com.zbkj.crmeb.enums.StockAreaTempEnum;
import com.zbkj.crmeb.store.model.OrderInfoDelivery;
import com.zbkj.crmeb.store.model.StoreBrand;
import com.zbkj.crmeb.store.model.Supplier;
import com.zbkj.crmeb.store.request.OrderInfoDeliveryRequest;
import com.zbkj.crmeb.store.service.OrderInfoDeliveryService;
import com.zbkj.crmeb.store.service.StoreBrandService;
import com.zbkj.crmeb.store.service.StoreSeasonService;
import com.zbkj.crmeb.store.service.SupplierService;
import com.zbkj.crmeb.system.service.SystemAdminService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Optional;

/**
 * 服务商-下拉列表
 * @author lvmd
 * @date : 2022-02-22
 */
@RestController
@RequestMapping("api/admin/mer/agent/store/select")
@Api(tags = "服务商-下拉列表")
public class MerAgentSelectController {

    @Autowired
    StoreSeasonService storeSeasonService;
    @Autowired
    private StoreBrandService storeBrandService;
    @Autowired
    private SupplierService supplierService;
    @Autowired
    private AgentService agentService;
    @Autowired
    private SystemAdminService systemAdminService;
    @Autowired
    private OrderInfoDeliveryService orderInfoDeliveryService;

    @ApiOperation(value = "季节列表")
    @GetMapping("/season/list")
    public CommonResult<Object> listEntity() {
        return CommonResult.success(storeSeasonService.selectEntityList());
    }

    @ApiOperation(value = "服务商品牌列表")
    @GetMapping("/brand/list")
    public CommonResult<Object> listEntity(@RequestParam(required = true) Integer type) {
        Agent agent = Optional.ofNullable(agentService.getById(systemAdminService.getAdminAgentId())).orElse(new Agent());
        JSONArray jsonArray = JSONArray.parseArray(agent.getBrandIds());
        return CommonResult.success(storeBrandService.list(Wrappers.<StoreBrand>lambdaQuery()
                .eq(StoreBrand::getType, BrandTypeEnum.valueOf(type))
                .in(!jsonArray.isEmpty(), StoreBrand::getId, jsonArray.toJavaList(Integer.class))
                .eq(StoreBrand::getStatus, true)
                .orderByAsc(StoreBrand::getLetter))
        );
    }

    @ApiOperation(value = "服务商供应商列表")
    @RequestMapping(value = "/getAllSupplier", method = RequestMethod.GET)
    public CommonResult<Object> getList() {
        Agent agent = Optional.ofNullable(agentService.getById(systemAdminService.getAdminAgentId())).orElse(new Agent());
        JSONArray jsonArray = JSONArray.parseArray(agent.getSuppliersIds());
        return CommonResult.success(supplierService.list(Wrappers.<Supplier>lambdaQuery()
                .in(!jsonArray.isEmpty(), Supplier :: getId, jsonArray.toJavaList(Integer.class) )
                .eq(Supplier::getIsDeleted, false)
                .isNull(Supplier::getMerId)
        ));
    }

    @ApiOperation(value = "获取服务商发货地列表")
    @GetMapping(value = "/stockArea")
    public CommonResult<List<StockAreaTempEnum>> getStockArea() {
        return CommonResult.success(agentService.getStockArea(systemAdminService.getAdminAgentId()));
    }

    @ApiOperation(value = "查询物流列表")
    @RequestMapping(value = "/deliveryList", method = RequestMethod.GET)
    public CommonResult<List<OrderInfoDelivery>> getDeliveryList(@Validated OrderInfoDeliveryRequest request) {
        return CommonResult.success(orderInfoDeliveryService.getDeliveryList(request));
    }
}
